-- Winter Anathema (c) v.v.balashoff
local Color = require('libs/colorlibrary/colorlibrary')
local Domain = require('leviathan/ECS/Domain')
local Entity = require('leviathan/ECS/Entity')
local i18n = require('libs/i18n')
local MouseClickComponent = require('leviathan/ECS/UI/Components/MouseClickComponent')
local MouseHoverComponent = require('leviathan/ECS/UI/Components/MouseHoverComponent')
local SliderOffsetComponent = require('leviathan/ECS/UI/Components/SliderOffsetComponent')
local TextComponent = require('leviathan/ECS/UI/Components/TextComponent')
local UILayoutSystem = require('leviathan/ECS/UI/Systems/LayoutSystem')
local UIRenderSystem = require('leviathan/ECS/UI/Systems/RenderSystem')
local UISliderOffsetSystem = require('leviathan/ECS/UI/Systems/SliderOffsetSystem')

local DomainAboutContent = Domain:derive('AboutContent')

function DomainAboutContent:new(world)
    -- call parent constructor
    DomainAboutContent.super.new(self, world, 'AboutContent')
    -- view settings
    self.view = true
    -- get screen size
    local screenW, screenH = love.window.getMode()
    -- get managers
    local FontsManager = world:getManager('FontsManager')
    -- fonts
    local fontTitleName = 'Rusted'
    local fontTextName = 'TypeWriter'
    local fontTitleSize = math.floor(screenH * 0.07) -- 7% of screen height
    local fontTitle = FontsManager:getFont(fontTitleName, fontTitleSize)
    fontTitleSize = fontTitle:getHeight()
    local fontTextSize = math.floor(screenH * 0.03) -- 3.0% of screen height
    local fontText = FontsManager:getFont(fontTextName, fontTextSize)
    fontTextSize = fontText:getHeight()
    -- coordinates
    local width = math.floor(screenW * 0.95) -- 96% of screen width
    local height = math.floor(screenH * 0.91) -- 91% of screen height
    local x = math.floor((screenW * 0.98 - width) * 0.5)
    local y = math.floor(screenH * 0.02) -- 02% of screen height
    -- slider coordinates (NOTE: should be sync with About!)
    local sliderW = math.floor(screenW * 0.015) -- 1.5% of screen width
    local sliderX = math.floor(screenW * 0.942) -- 94.2% of screen width
    local sliderY = math.floor(screenH * 0.05) -- 5% of screen width
    local sliderH = math.floor(screenH * 0.89) -- 89% of screen height
    -- set buffer size
    self.buffer.worldX = x
    self.buffer.worldY = y
    self.buffer.w = width
    self.buffer.h = height
    -- content
    local contentW = math.floor(screenW * 0.93) -- 93% of screen width
    -- title
    local titleX = fontTextSize * 1.5
    local titleY = fontTextSize * 0.5
    local strTitle = i18n.translate('About')
    local titleW, titleH = FontsManager:getTextSize(fontTitle, strTitle, contentW)
    -- text
    local textX = 0
    local textY = titleH * 1.5
    local strText = i18n.translate('AboutProject')
    local textW, textH = FontsManager:getTextSize(fontText, strText, contentW)
    -- entity: content
    local content = Entity(self, 'Content', 0, 0, contentW, height)
    -- entity: title
    local title = Entity(self, 'Title', titleX, titleY, titleW, fontTitleSize)
    local titleComp = TextComponent('TitleTextComponent', 0, 0, contentW, fontTitleSize, strTitle, fontTitle, Color.black, 'left')
    title:addComponent(titleComp)
    content:addChild(title)
    -- entity: text
    local text = Entity(self, 'Text', textX, textY, contentW, height)
    local textComp = TextComponent('TextComponent', 0, 0, contentW, height, strText, fontText, Color.black, 'left')
    text:addComponent(textComp)
    content:addChild(text)
    -- set view in parent domain
    local aboutDomain = world:getDomain('About')
    local viewerEntity = aboutDomain:getEntity('Viewer')
    local vComp = viewerEntity:getComponent('ViewerComponent')
    vComp.source = self
    -- slider text offset
    local textOffset = Entity(self, 'TextOffset', sliderX, sliderY, sliderW, sliderH)
    -- get sliderComp from About domain
    local sliderEntity = aboutDomain:getEntity('Scroller')
    local sliderComp = sliderEntity:getComponent('SliderComponent')
    local textOffsetComponent = SliderOffsetComponent('TextOffsetComponent', sliderX, sliderY, sliderW, -height, sliderComp, content, 'y')
    textOffset:addComponent(textOffsetComponent)
    -- systems
    local layoutSys = UILayoutSystem(self)
    local sliderOffsetSys = UISliderOffsetSystem(self)
    local renderSys = UIRenderSystem(self)
    -- adding systems
    DomainAboutContent:addSystem(self, layoutSys, 420)
    DomainAboutContent:addSystem(self, sliderOffsetSys)
    DomainAboutContent:addSystem(self, renderSys)
end

return DomainAboutContent
